package ServletController;


import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import classeMetier.DataManager;








public class BaseServlet extends javax.servlet.http.HttpServlet
    implements javax.servlet.Servlet {
  private static final long serialVersionUID = 1L;


  public BaseServlet() {
    super();
    }
  public void init(ServletConfig config) throws ServletException {
	    System.out.println("*** initializing controller servlet.");
	    super.init(config);

	    DataManager dataManager = new DataManager();
	    dataManager.setDbURL(config.getInitParameter("dbURL"));
	    dataManager.setDbUserName(config.getInitParameter("dbUserName"));
	    dataManager.setDbPassword(config.getInitParameter("dbPassword"));

	    ServletContext context = config.getServletContext();
	    context.setAttribute("base", config.getInitParameter("base"));
	    context.setAttribute("dataManager", dataManager);

	    try {  // load the database JDBC driver
	      Class.forName(config.getInitParameter("jdbcDriver"));
	      }
	    catch (ClassNotFoundException e) {
	      System.out.println(e.toString());
	      }
	    }

	  protected void doGet(HttpServletRequest request,
	      HttpServletResponse response) throws ServletException, IOException {
	    doPost(request, response);
	    }


  protected void doPost(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {
    String base = "/";
    String url = base + "listedesmessages.jsp";
    String action = request.getParameter("action");
    if (action != null) {
      if (action.equals("ajouterutilisateur"))
        url = base + "ajouterutilisateur.jsp";
      else if (action.equals("creermessage"))
        url = base + "creermessage.jsp";
      else if (action.equals("listedesmessages"))
        url = base + "listedesmessages.jsp";
    }
    RequestDispatcher requestDispatcher =
      getServletContext().getRequestDispatcher(url);
    requestDispatcher.forward(request, response);
    }
  }
